package com.lanhe.product.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanhe.common.constant.Constants;
import com.lanhe.common.constant.HttpStatus;
import com.lanhe.common.exception.ServiceException;
import com.lanhe.product.domain.LhLevelInfo;
import com.lanhe.product.mapper.LhLevelInfoMapper;
import com.lanhe.product.service.ILhLevelInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 等级表(LhLevelInfo)表服务实现类
 *
 * @author zhanggl
 * @since 2024-01-04 20:23:46
 */
@Service
public class LhLevelInfoServiceImpl extends ServiceImpl<LhLevelInfoMapper, LhLevelInfo> implements ILhLevelInfoService {

	@Autowired
	private LhLevelInfoMapper lhLevelInfoMapper;

	/**
	 * 获取可购买的等级数据
	 *
	 * @param leaderLevel 等级
	 * @return com.lanhe.product.domain.LhLevelInfo
	 * @author zhanggl
	 * @date 2024/1/4
	 */
	@Override
	public List<LhLevelInfo> getLeveInfoProduct(Integer leaderLevel) {
		LambdaQueryWrapper<LhLevelInfo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.apply("FIND_IN_SET({0}, level_show)", leaderLevel);
		return lhLevelInfoMapper.selectList(queryWrapper);
	}

	/**
	 * 分页查询
	 *
	 * @param lhLevelInfo 实体
	 * @return java.util.List<com.lanhe.product.domain.LhLevelInfo>
	 * @author zhanggl
	 * @date 2024/1/17
	 */
	@Override
	public List<LhLevelInfo> selectLevelInfoList(LhLevelInfo lhLevelInfo) {
		LambdaQueryWrapper<LhLevelInfo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LhLevelInfo::getDelFlag, Constants.GeneralIntType.ZERO);
		queryWrapper.orderByAsc(LhLevelInfo::getLevelId);
		return lhLevelInfoMapper.selectList(queryWrapper);
	}

	/**
	 * 根据编号获取详细信息
	 *
	 * @param levelId 会员等级ID
	 * @return com.lanhe.product.domain.LhLevelInfo
	 * @author zhanggl
	 * @date 2024/1/17
	 */
	@Override
	public LhLevelInfo selectLevelInfoById(Long levelId) {
		LambdaQueryWrapper<LhLevelInfo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LhLevelInfo::getDelFlag, 0)
				.eq(LhLevelInfo::getLevelId, levelId);
		return lhLevelInfoMapper.selectOne(queryWrapper);
	}

	/**
	 * 插入
	 *
	 * @param levelInfo 实体
	 * @return int
	 * @author zhanggl
	 * @date 2024/1/17
	 */
	@Override
	public int insertLevelInfo(LhLevelInfo levelInfo) {
		LhLevelInfo lhLevelInfo = selectLevelInfoByLeaderLevel(levelInfo.getLeaderLevel());
		if (ObjectUtil.isNotNull(lhLevelInfo)){
			throw new ServiceException("会员等级："+levelInfo.getLeaderLevel()+"已经存在，请勿重复添加", HttpStatus.ERROR);
		}
		levelInfo.setDelFlag(Constants.GeneralIntType.ZERO+"");
		return lhLevelInfoMapper.insert(levelInfo);

	}

	/**
	 * 修改
	 *
	 * @param levelInfo 实体
	 * @return int
	 * @author zhanggl
	 * @date 2024/1/17
	 */
	@Override
	public int updateLevelInfo(LhLevelInfo levelInfo) {

		LhLevelInfo lhLevelInfo = selectLevelInfoByLeaderLevel(levelInfo.getLeaderLevel());
		if (ObjectUtil.isNotNull(lhLevelInfo) && !lhLevelInfo.getLevelId().equals(levelInfo.getLevelId())){
			throw new ServiceException("会员等级："+levelInfo.getLeaderLevel()+"已经存在，请勿重复添加", HttpStatus.ERROR);
		}
		LambdaQueryWrapper<LhLevelInfo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LhLevelInfo::getDelFlag, 0)
				.eq(LhLevelInfo::getLevelId, levelInfo.getLevelId());
		return lhLevelInfoMapper.update(levelInfo, queryWrapper);
	}

	/**
	 * 删除
	 *
	 * @param levelIds ID
	 * @return int
	 * @author zhanggl
	 * @date 2024/1/17
	 */
	@Override
	public int deleteLevelInfoByIds(Long[] levelIds) {
		LambdaUpdateWrapper<LhLevelInfo> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper.set(LhLevelInfo::getDelFlag, Constants.GeneralIntType.TWO + "")
				.in(LhLevelInfo::getLevelId, levelIds);
		return lhLevelInfoMapper.update(null, updateWrapper);
	}


	/**
	 * 根据VIP等级查询
	 *
	 * @param leaderLevel 实体
	 * @return com.lanhe.product.domain.LhLevelInfo
	 * @author zhanggl
	 * @date 2024/1/18
	 */
	@Override
	public LhLevelInfo selectLevelInfoByLeaderLevel(Integer leaderLevel) {
		LambdaQueryWrapper<LhLevelInfo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LhLevelInfo::getDelFlag, 0)
				.eq(LhLevelInfo::getLeaderLevel, leaderLevel);
		return lhLevelInfoMapper.selectOne(queryWrapper);
	}
}

